package com.woniuxy.amusementparkos.service.lvd.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.amusementparkos.dao.lvd.FinancialStatisticsDao;
import com.woniuxy.amusementparkos.entity.lvd.CompulsorySettlement;
import com.woniuxy.amusementparkos.entity.lvd.ExpenditureRecord;
import com.woniuxy.amusementparkos.entity.lvd.MerchantTransactions;
import com.woniuxy.amusementparkos.entity.lvd.RevenueAndProfitRecord;
import com.woniuxy.amusementparkos.entity.lvd.RevenueRecord;
import com.woniuxy.amusementparkos.entity.lvd.StoreTypeRevenueVisits;
import com.woniuxy.amusementparkos.entity.lvd.SummaryReport;
import com.woniuxy.amusementparkos.service.lvd.FinancialStatisticsService;

@Service
public class FinancialStatisticsServiceImpl implements FinancialStatisticsService {
	@Autowired
	FinancialStatisticsDao financialStatisticsDao;

	@Override
	public List<SummaryReport> selectPage(int page, int pageSize) {
		return financialStatisticsDao.selectPage(page, pageSize);
	}

	@Override
	public long selectRecordCount() {
		return financialStatisticsDao.selectRecordCount();
	}

	@Override
	public void insertSummaryReport(SummaryReport summaryReport) {
		financialStatisticsDao.insertSummaryReport(summaryReport);
	}

	@Override
	public void updateSummaryReport(SummaryReport summaryReport) {
		financialStatisticsDao.updateSummaryReport(summaryReport);
	}

	@Override
	public void deleteSummaryReport(String id) {
		financialStatisticsDao.deleteSummaryReport(id);
	}

	@Override
	public Map<String, String> selectSummaryDate() {
		// 查询所有日期
		List<String> dates = financialStatisticsDao.selectSummaryDate();
		Calendar calendar = Calendar.getInstance();
		// 定义日期格式
		SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, String> weeks = new LinkedHashMap<String, String>();
		Map<String, String> weeksReverse = new LinkedHashMap<String, String>();
		try {
			for (String str : dates) {
				calendar.setTime(dformat.parse(str));
				// 获取当前日期的星期
				int week = calendar.get(Calendar.DAY_OF_WEEK);
				// 判断当前日期是否为星期日
				if (week == 1) {
					calendar.setTime(dformat.parse(str));
					long timeInMillis = calendar.getTimeInMillis();
					// 根据星期日的日期计算出同一周的星期一的日期
					long monday = timeInMillis - 24 * 6 * 60 * 60 * 1000;
					calendar.setTimeInMillis(monday);
					weeks.put(str, dformat.format(calendar.getTime()));
				}

			}
			// weeks倒序
			ArrayList<String> tmp = new ArrayList<String>(weeks.keySet());
			Collections.reverse(tmp);
			for (String string : tmp) {
				weeksReverse.put(string, weeks.get(string));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return weeksReverse;
	}

	@Override
	public List<SummaryReport> selectWeekSummaryReports(String monday, String sunday) {
		return financialStatisticsDao.selectWeekSummaryReports(monday, sunday);
	}

	@Override
	public double selectTotalRecharge() {
		return financialStatisticsDao.selectTotalRecharge();
	}

	@Override
	public double selectTotalConsumption() {
		return financialStatisticsDao.selectTotalConsumption();
	}

	@Override
	public double selectTotalRevenue() {
		return financialStatisticsDao.selectTotalRevenue();
	}

	@Override
	public double selectTotalExpenditure() {
		return financialStatisticsDao.selectTotalExpenditure();
	}

	@Override
	public List<RevenueAndProfitRecord> selectWeekRevenueAndProfitRecord(String monday, String sunday) {
		List<RevenueAndProfitRecord> revenueAndProfitRecords = new ArrayList<RevenueAndProfitRecord>();
		// 查询一周之内的收入数据
		List<RevenueRecord> weekRevenue = financialStatisticsDao.selectWeekRevenue(monday, sunday);
		// 查询一周之内的支出数据
		List<ExpenditureRecord> weekExpenditure = financialStatisticsDao.selectWeekExpenditure(monday, sunday);
		for (int i = 0; i < weekRevenue.size(); i++) {
			RevenueRecord tmpRevenue = weekRevenue.get(i);
			ExpenditureRecord tmpExpenditure = weekExpenditure.get(i);
			// 计算总收入(线下收入+线上收入)
			BigDecimal totalRevenue = tmpRevenue.getDaily_online_revenue().add(tmpRevenue.getDaily_offline_revenue());
			revenueAndProfitRecords.add(new RevenueAndProfitRecord(tmpRevenue.getStatistics_date(), totalRevenue,
					totalRevenue.subtract(tmpExpenditure.getDaily_expenditure())));
		}
		return revenueAndProfitRecords;
	}

	@Override
	public List<StoreTypeRevenueVisits> selectStoreTypeRevenueVisits() {
		return financialStatisticsDao.selectStoreTypeRevenueVisits();
	}

	@Override
	public List<List<StoreTypeRevenueVisits>> selectWeekStoreTypeRevenueVisits() {
		List<List<StoreTypeRevenueVisits>> resultList = new ArrayList<List<StoreTypeRevenueVisits>>();
		Calendar calendar = Calendar.getInstance();
		// 定义日期格式
		SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
		// 查询订单表日期
		List<String> orderDate = financialStatisticsDao.selectOrderDate();
		int size = orderDate.size();
		String monday = "";
		List<StoreTypeRevenueVisits> weekStoreTypeRevenueVisits = null;
		try {
			for (int i = size - 1; i >= 0; i--) {
				calendar.setTime(dformat.parse(orderDate.get(i)));
				// 获取当前日期的星期
				int weekdate = calendar.get(Calendar.DAY_OF_WEEK);
				if (weekdate == 1) {
					long timeInMillis = calendar.getTimeInMillis();
					// 根据星期日的日期计算出同一周的星期一的日期
					long mondaydate = timeInMillis - 24 * 6 * 60 * 60 * 1000;
					calendar.setTimeInMillis(mondaydate);
					monday = dformat.format(calendar.getTime());
					break;
				}
			}
			for (int i = 1; i <= 7; i++) {
				calendar.setTime(dformat.parse(monday));
				long timeInMillis = calendar.getTimeInMillis();
				long tmpdate1 = timeInMillis + 24 * (i - 1) * 60 * 60 * 1000;
				long tmpdate2 = timeInMillis + 24 * i * 60 * 60 * 1000;
				calendar.setTimeInMillis(tmpdate2);
				// 查询一周内每一天的商户收入与访问人次
				weekStoreTypeRevenueVisits = financialStatisticsDao.selectWeekStoreTypeRevenueVisits(
						dformat.format(new Date(tmpdate1)), dformat.format(calendar.getTime()));
				resultList.add(weekStoreTypeRevenueVisits);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultList;
	}

	@Override
	public List<String> selectOrderDate() {
		List<String> orderDate = financialStatisticsDao.selectOrderDate();
		List<String> resultList = new ArrayList<String>();
		for (String date : orderDate) {
			resultList.add(date.substring(0, 10));
		}
		return resultList;
	}

	@Override
	public List<MerchantTransactions> selectMerchantTransactions(String begin, String end) {
		return financialStatisticsDao.selectMerchantTransactions(begin, end);
	}

	@Override
	public List<CompulsorySettlement> selectCompulsorySettlementByCondition(String start, String end, String status,
			String handle, String wriststrpid, int page, int pageSize) {
		return financialStatisticsDao.selectCompulsorySettlementByCondition(start, end, status, handle, wriststrpid,
				page, pageSize);
	}

	@Override
	public Long selectCompulsorySettlementCount() {
		return financialStatisticsDao.selectCompulsorySettlementCount();
	}

}
